# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: Bruno Pagani <archange@archlinux.org>

pkgname=matrix-appservice-irc
pkgver=0.38.0
pkgrel=1
pkgdesc="Node.js IRC bridge for Matrix"
arch=(loong64 x86_64)
url="https://github.com/matrix-org/matrix-appservice-irc"
license=(Apache)
depends=(
  gcc-libs
  glibc
  nodejs
)
makedepends=(
  git
  npm
)
backup=(
  etc/$pkgname/config.yaml
  etc/$pkgname/registration.yaml
)
install=$pkgname.install
source=(
  https://github.com/matrix-org/matrix-appservice-irc/archive/$pkgver/$pkgname-$pkgver.tar.gz
  $pkgname.service
  $pkgname.sysusers
  $pkgname.tmpfiles
)
sha512sums=('f088b42d07783e7d72d7edb7a38d55d67ab8d19d775767ffbef9b126103b5d5de9a3334a998a154b26bb6ded22dd59f6d9011ba644456c4b7f245c422e99ebf7'
            'c4b216a6b66c03a069f2e013ac299401cf8786a070b9b7a25401cb6ba3e877e411b011ff18e8037558c04cfb801d292278fce883931999f29cb7a2e9001a3157'
            'f615e5da11be8d834d6f48f064a65a9b7aa129d255be7865a171522d5aed921eb549a33661ab2c4fff4625d2ca5a7f9c43be95c188e7a77bdde4062aa3983edd'
            '60b7afdd68aaf2c8e47caf10efefb6c4dc54f40d187ad495a604786b30c00dac7e5c77a7b596d86c2a62a7cdbe3727e11f75494f86d26c9fc51ea7a1bf6ab7f0')
b2sums=('1ba6ff5af6ca219b0ddc955ce506d4cb86d1f763c85f728795401830bfafbce6303834a78d0a01bbbc869810442287a1344c9bb8d0286266106db06e9ba278f8'
        '279ccd483007fa1a15bd113fabffe4f2c5e089a120baba4a35698dca7dabe86938bec33af20dc5700241908d6ddc8971a5c1a5da3a3b50b52310034dd7b7ab4b'
        'cbfe3cdb07707b79ae438936927f06ac4e92669b0b7dd35a9d228aae3cc821e3a50a2cc1627f7959147e329e617247f4002a267bc8630819946e8083a39b8895'
        '2dbbf14f82d88418470f848a2d11c0feacb257026382513c2fa02ad7f605f47e4cc5e9bf05668da383a596bc81450e468288863cdbdde9f7c16b02561f0114a7')

prepare() {
  cd $pkgname-$pkgver
  touch registration.yaml
}

build() {
  cd $pkgname-$pkgver
  # TODO: fix issues with full RELRO and PIE
  # https://github.com/matrix-org/matrix-appservice-irc/issues/1133
  npm install --cache "$srcdir"/npm-cache
}

check() {
  cd $pkgname-$pkgver
  npm audit || echo "npm audit output might return non-zero"
  npm test
}

package() {
  cd $pkgname-$pkgver
  # removing unneeded files and directories
  find node_modules -type f \
          \( \
         -iname '*Makefile*' -o \
         -iname '*appveyor.yml' -o \
         -iname '*.babelrc' -o \
         -iname '*.bak' -o \
         -iname '*bower.json' -o \
         -iname '*.c' -o \
         -iname '*.cc' -o \
         -iname '*.cpp' -o \
         -iname '*.md' -o \
         -iname '*.markdown' -o \
         -iname '*.rst' -o \
         -iname '*.nycrc' -o \
         -iname '*.npmignore' -o \
         -iname '*.editorconfig' -o \
         -iname '*.el' -o \
         -iname '*.eslintignore' -o \
         -iname '*.eslintrc*' -o \
         -iname '*.fimbullinter.yaml' -o \
         -iname '*.gitattributes' -o \
         -iname '*.gitmodules' -o \
         -iname '*.h' -o \
         -iname '*.html' -o \
         -iname '*.jshintrc' -o \
         -iname '*.jscs.json' -o \
         -iname '*.log' -o \
         -iname '*logo.svg' -o \
         -iname '*.nvmrc' -o \
         -iname '*.o' -o \
         -iname '*package-lock.json' -o \
         -iname '*.travis.yml' -o \
         -iname '*.prettierrc' -o \
         -iname '*.sh' -o \
         -iname '*.tags*' -o \
         -iname '*.tm_properties' -o \
         -iname '*.wotanrc.yaml' -o \
         -iname '*tsconfig.json' -o \
         -iname '*yarn.lock' \
         \) \
         -delete
  find node_modules -type d \
          \( \
         -iwholename '*.github' -o \
         -iwholename '*.tscache' -o \
         -iwholename '*/man' -o \
         -iwholename '*/test' -o \
         -iwholename '*/scripts' -o \
         -iwholename '*/git-hooks' \
         \) \
         -exec rm -rvf {} +
  find node_modules -empty -type d -delete
  install -vdm 755 "$pkgdir/usr/lib/node_modules/$pkgname/"
  # copy vendored modules, lib and entry point
  cp -av {lib,node_modules,app.js,config.schema.yml} "$pkgdir/usr/lib/node_modules/$pkgname/"
  # configuration
  install -vDm 640 config.sample.yaml "$pkgdir/etc/$pkgname/config.yaml"
  install -vDm 640 registration.yaml -t "$pkgdir/etc/$pkgname/"
  # service
  install -vDm 644 ../$pkgname.service -t "$pkgdir/usr/lib/systemd/system"
  # tmpfiles.d and sysusers.d
  install -vDm 644 ../$pkgname.sysusers "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"
  install -vDm 644 ../$pkgname.tmpfiles "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"
  # add scripts (which are location dependent)
  install -vDm 755 scripts/*.sh -t "$pkgdir/usr/lib/node_modules/$pkgname/scripts"
  # docs
  install -vDm 644 {CHANGELOG,CONTRIBUTING,HOWTO,README}.md -t "$pkgdir/usr/share/doc/$pkgname"
}
